let elementStyle = document.createElement('div').style
let vendor = (function () {
  let transformNames = {
    webkit: 'webkitTransform',
    Moz: 'MozTransform',
    O: 'OTransform',
    ms: 'msTransform',
    standard: 'transform'
  }

  for (let key in transformNames) {
    if (elementStyle[transformNames[key]] !== undefined) {
      return key
    }
  }

  return false
})()

function prefixStyle (style) {
  if (vendor === false) {
    return false
  }

  if (vendor === 'standard') {
    return style
  }

  return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}

export const style = {
  transform: prefixStyle('transform'),
  transitionDuration: prefixStyle('transitionDuration'),
  transitionTimingFunction: prefixStyle('transitionTimingFunction')
}

export function addDomEvent (el, type, fn, capture) {
  el.addEventListener(type, fn, {
    capture: !!capture,
    passive: false
  })
}

export function removeDomEvent (el, type, fn, capture) {
  el.removeEventListener(type, fn, {
    capture: !!capture,
    passive: false
  })
}

// 注意这里取的是负值
export function offset (el) {
  let left = 0
  let top = 0

  while (el) {
    left -= el.offsetLeft
    top -= el.offsetTop
    el = el.offsetParent
  }

  return {
    left,
    top
  }
}

export const TOUCH_EVENT = 1
export const MOUSE_EVENT = 2

export const eventType = {
  touchstart: TOUCH_EVENT,
  touchmove: TOUCH_EVENT,
  touchend: TOUCH_EVENT,

  mousedown: MOUSE_EVENT,
  mousemove: MOUSE_EVENT,
  mouseup: MOUSE_EVENT
}

export function preventDefaultException (el, exceptions) {
  for (let i in exceptions) {
    if (exceptions[i].test(el[i])) {
      return true
    }
  }
  return false
}
